package com.liao.starapp.star;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;

public class StarViewPagerAdapter extends PagerAdapter {
    private ArrayList<View> views;
    private Context context;

    public StarViewPagerAdapter(Context context, ArrayList<View> views) {
        this.views = views;
        this.context = context;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        final int i = position % views.size();
        View view = views.get(i);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("StarViewPagerAdapter.onClick" + i);
                switch (i) {
                    case 1:
                    case 3:
                        Toast.makeText(context, "广告", Toast.LENGTH_SHORT).show();
                        break;
                    case 0:
                    case 2:
                        context.startActivity(new Intent(context, StarDiagramActivity.class));
                        break;
                }
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position % views.size()));
    }
}
